<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button type="primary" @click="add">+ 新增角色</el-button>
          <el-table :data="list">
            <el-table-column label="序号">
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name"></el-table-column>
            <el-table-column label="描述" prop="description"></el-table-column>
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button type="text" @click="setPower(row)"
                    >分配权限</el-button
                  >
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <el-button type="text" @click="del(row)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination">
            <el-pagination
              @size-change="sizeChange"
              @current-change="currentChange"
              :current-page="page.page"
              :page-sizes="[1, 3, 5, 10, 20, 30]"
              :page-size="page.pagesize"
              layout="total,sizes,prev,pager,next,jumper"
              :total="page.total"
            ></el-pagination>
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息">
          <!-- 动态组件，结合异步组件使用 -->
          <component :is="'Info'"></component>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- <DialogItem :isShow="show" @input="show = $event"></DialogItem> -->
    <!-- <DialogItem v-model="show"></DialogItem> -->
    <!-- <DialogItem :isShow="show" @update:isShow="show = $event"></DialogItem> -->
    <DialogItem
      ref="dialogItem"
      :isShow.sync="show"
      :list="list"
      @currentChange="currentChange"
      @getData="getData"
    ></DialogItem>
    <!-- 分配权限 -->
    <DialogPower :isShow.sync="showPower" ref="dialogPower"></DialogPower>
  </div>
</template>

<script>
import DialogPower from './components/dialogPower'
import { sysRole, sysRoleDelete } from '@/api/setting.js'
import DialogItem from './components/dialogitem.vue'
export default {
  components: {
    DialogPower,
    DialogItem,
    Info: () => import('./components/info.vue') // 异步组件
  },
  data () {
    return {
      showPower: false,
      show: false,
      page: {
        page: 1,
        pagesize: 10,
        total: 100
      },
      list: []
    }
  },
  created () {
    this.getData()
  },
  methods: {
    // 进入页面调用接口获取数据并存储
    async getData () {
      const res = await sysRole(this.page)
      console.log('角色列表', res)
      this.page.total = res.data.total
      this.list = res.data.rows
    },
    // 页码变更
    currentChange (page) {
      this.page.page = page
      this.getData()
    },
    // 页容量变更
    sizeChange (size) {
      this.page.pagesize = size
      this.page.page = 1
      this.getData()
    },
    // 新增角色按钮
    add () {
      this.show = true
      this.$refs.dialogItem.mode = 'add'
    },
    // 删除按钮
    del (row) {
      this.$confirm('请确定删除该角色?', '提示')
        .then(async () => {
          await sysRoleDelete(row.id)
          this.$message.success('删除成功')
          this.currentChange(1)
        })
        .catch(() => {})
      console.log('点击删除行id', row)
    },
    // 修改按钮
    edit (row) {
      this.show = true
      this.$refs.dialogItem.form = JSON.parse(JSON.stringify(row))
      this.$refs.dialogItem.mode = 'edit'
    },
    //  分配权限
    setPower (row) {
      this.showPower = true
      // 打开设置权限弹框时，调用角色详情
      this.$refs.dialogPower.getRoleInfo(row.id)
    }
  }
}
</script>

<style lang="scss">
.setting {
  padding: 15px;
  .pagination {
    text-align: right;
    padding-top: 15px;
  }
}
</style>
